package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.Admin;
import com.itheima.mapper.AdminMapper;
import com.itheima.service.db.AdminService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.HttpUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin findByName(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 微信登录
     * @param code
     * @return
     */
    //@Value("${wx.appid}")
    private static String appid = "wx3bdb1192c22883f3";
    //@Value("${wx.secret}")
    private static String secret = "db9d6b88821df403e5ff11742e799105";
    //@Value("${wx.accessTokenUrl}")
    private static String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
    //@Value("${wx.wxInfoUrl}")
    private static String wxInfoUrl = "https://api.weixin.qq.com/sns/userinfo";


    @Override
    public Admin wxLogin(String code)  {

        Admin admin = null;
        System.out.println(code);
        //根据code获取access_token和openId
        //String atUtl = accessTokenUrl + "?code="+code+"&appid="+appid+"&secret="+secret+"&grant_type=authorization_code";
        //String atUtl = "https://api.weixin.qq.com/sns/oauth2/access_token" + "?code="+code+"&appid="+"wx3bdb1192c22883f3"+"&secret="+"db9d6b88821df403e5ff11742e799105"+"&grant_type=authorization_code";
        String atUtl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3bdb1192c22883f3&secret=db9d6b88821df403e5ff11742e799105&code="+code+"&grant_type=authorization_code";
        Map<String, Object> map = HttpUtils.sendGet(atUtl);
        System.err.println("map："+map);
        Object access_token = map.get("access_token");
        System.err.println("access_token："+access_token);
        Object openid = map.get("openid");
        System.err.println("openid："+openid);
        if (access_token == null && openid == null) {
            return admin;
        }
        //System.out.println("map2："+map);
        //根据openId判断用户是否存在
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openid);
        admin = adminMapper.selectOne(queryWrapper);
        if (admin != null) {
            System.out.println("返回数据库中的用户对象");
            //如果存在返回信息
            return admin;
        } else {
            System.out.println("查询微信数据库");
            //如果用户不存在根据access_token和openid获取微信用户信息
            String wxurl = "https://api.weixin.qq.com/sns/userinfo" + "?access_token=" + access_token +"&openid="+openid;
            Map<String, Object> map2 = HttpUtils.sendGet(wxurl);
            //昵称
            Object nickname = map2.get("nickname");
            System.err.println("nickname："+nickname);
            //头像
            Object headimgurl = map2.get("headimgurl");
            System.err.println("headimgurl："+headimgurl);
            if (nickname == null || headimgurl == null) {
                return admin;
            }
            //将微信用户信息保存到数据库，返回用户数据
            admin = new Admin();
            admin.setAvatar(headimgurl.toString());
            admin.setUsername(nickname.toString());
            admin.setPassword(ConstantUtil.INIT_PASSWORD);
            admin.setOpenId(openid.toString());
            System.err.println("admin："+admin);
            //这里只在页面展示，不保存到数据库
            adminMapper.insert(admin);
            return admin;
        }

    }
}
